import os
from pathlib import Path
from typing import Union
from app.config import ALLOWED_EXTENSIONS

def allowed_file(filename: str, file_type: str = "image") -> bool:
    """检查文件扩展名是否允许"""
    return "." in filename and \
           filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS[file_type]

def get_file_extension(filename: str) -> str:
    """获取文件扩展名"""
    return filename.rsplit(".", 1)[1].lower()

def generate_output_filename(input_filename: str, suffix: str = "_processed") -> str:
    """生成输出文件名"""
    ext = get_file_extension(input_filename)
    base_name = os.path.basename(input_filename).rsplit(".", 1)[0]
    return f"{base_name}{suffix}.{ext}"

def cleanup_file(file_path: Union[str, Path]) -> None:
    """清理文件"""
    try:
        if os.path.exists(file_path):
            os.remove(file_path)
    except Exception as e:
        print(f"Error cleaning up file {file_path}: {e}")